package com.sharemarking.wa.platform.service.OrderCommonFactory;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.Order;
import com.sharemarking.wa.common.entity.OrgMember;
import com.sharemarking.wa.common.eum.OrderOperateTypeEum;
import com.sharemarking.wa.common.eum.OrderStateEum;
import com.sharemarking.wa.platform.dto.driverVerify.DriverVerifyDto;
import com.sharemarking.wa.platform.dto.orderCommon.OrderInfoDto;
import com.sharemarking.wa.platform.dto.orderCommon.OrderListDto;
import com.sharemarking.wa.platform.dto.orderCommon.TaskOrderListDto;
import com.sharemarking.wa.platform.dto.orderCommon.WasteListDto;
import com.sharemarking.wa.platform.mapper.*;
import com.sharemarking.wa.platform.service.SecurityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Component
public class OrderDispatch implements OrderCommon {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private OrgMapper orgMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private OrgMemberMapper orgMemberMapper;
    @Autowired
    private DriverVerifyMapper driverVerifyMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderCommonMapper orderCommonMapper;


    @Override
    public List<OrderListDto> getOrderList(Map params) {
        Integer role = (Integer) params.get("role");
        if (role.equals(4)) {
            Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().eq("member_id", params.get("memberId"))).getOrgId();
            params.put("orgId", orgId);
        }
        List<OrderListDto> list = orderCommonMapper.getList(params);

        for (OrderListDto orderListDto : list) {
            orderListDto.setWasteList(orderCommonMapper.getWasteList(orderListDto.getId()));
            orderListDto.setLoadingAddress(orderAddressMapper.getAddressByOrderId(orderListDto.getParentId(), 0));
            orderListDto.setUnLoadingAddress(orderAddressMapper.getAddressByOrderId(orderListDto.getId(), 1));

            //操作权限
            if (orderListDto.getState().equals(OrderStateEum.DRIVER_CONFIRM.getKey())) {
                //待司机确认 可以更换司机 和 取消订单
                List<OrderOperateTypeEum> eumList = new ArrayList<>();
                eumList.add(OrderOperateTypeEum.REPLACE);
                eumList.add(OrderOperateTypeEum.CANCEL);
                orderListDto.setOperate(eumList);
            }

            //设置pc端的废品名称
            if (orderListDto.getWasteList().size() != 0) {
                String[] strings = new String[orderListDto.getWasteList().size()];
                for (int i = 0, length = orderListDto.getWasteList().size(); i < length; i++) {
                    strings[i] = orderListDto.getWasteList().get(i).getWasteName();
                }
                orderListDto.setWasteListName(String.join("、", strings));
            }

        }
        return list;
    }

    @Override
    public OrderInfoDto getOrderInfo(Map params) {
        String code = String.valueOf(params.get("code"));

        Order order = orderMapper.selectOne(new QueryWrapper<Order>().eq("code", code));
        OrderInfoDto orderInfoDto = new OrderInfoDto();

        if (order != null) {
            BeanUtils.copyProperties(order, orderInfoDto);
        }

        //子订单
        //获取装货地址
        String loadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 0);
        //获取卸货地址
        String unLoadingAddress = orderAddressMapper.getAddressByOrderId(order.getId(), 1);

        orderInfoDto.setLoadingAddress(loadingAddress);
        orderInfoDto.setUnLoadingAddress(unLoadingAddress);

        //完成时间 和 到达时间
        orderInfoDto.setArrivalTime(order.getArrivalTime());
        orderInfoDto.setCompleteTime(order.getCompleteTime());

        //获取经营单位名称
        if (!StringUtils.isEmpty(order.getReceiveOrgId())) {
            orderInfoDto.setOrgName(orgMapper.getOrgNameById(order.getReceiveOrgId()));
        }

        //获取经营单位联系人姓名和电话
        orderInfoDto.setCarWeight(order.getCarWeight());
        if (!StringUtils.isEmpty(order.getReceiveOrgMemberId())) {
            Member member = memberMapper.get(order.getReceiveOrgMemberId());
            if (member.getRealName() != null) {
                orderInfoDto.setContact(member.getRealName());
            } else {
                orderInfoDto.setContact(member.getNickName());
            }
            orderInfoDto.setMobile(member.getMobile());
        }

        //获取运输单位名称
        if (!StringUtils.isEmpty(order.getTransportOrgId())) {
            orderInfoDto.setTransportOrgName(orgMapper.getOrgNameById(order.getTransportOrgId()));
        }

        //获取司机名称和电话
        orderInfoDto.setCarWeight(order.getCarWeight());
        if (!StringUtils.isEmpty(order.getDriverId())) {
            Member member = memberMapper.get(order.getDriverId());
            if (member != null) {
                orderInfoDto.setDriverName(member.getRealName() != null?member.getRealName():member.getNickName());
                orderInfoDto.setDriverMobile(member.getMobile());
            }
            orderInfoDto.setCarNumber(driverVerifyMapper.getCarMemberIdByMemberId(order.getDriverId()));
        }


        //获取废品列表
        List<WasteListDto> childOrderWasteListDtos = orderCommonMapper.getChildOrderWasteListByOrderId(order.getId());
        orderInfoDto.setWasteList(childOrderWasteListDtos);

        Member member;
        //获取产废单位信息
        orderInfoDto.setWasteOrgName(orgMapper.getOrgNameById(order.getSendOrgId()));
        //获取产废单位联系人 手机号码
        member = memberMapper.get(order.getSendOrgMemberId());
        if (member.getRealName() != null) {
            orderInfoDto.setWasteContact(member.getRealName());
        } else {
            orderInfoDto.setWasteContact(member.getNickName());
        }
        orderInfoDto.setWasteMobile(member.getMobile());

        //获取经营单位联系人姓名和电话
        if (!StringUtils.isEmpty(order.getReceiveOrgMemberId())) {
            member = memberMapper.get(order.getReceiveOrgMemberId());
            if (member.getRealName() != null) {
                orderInfoDto.setContact(member.getRealName());
            } else {
                orderInfoDto.setContact(member.getNickName());
            }
            orderInfoDto.setMobile(member.getMobile());
        }

        //操作权限
        List<OrderOperateTypeEum> eumList = new ArrayList<>();
        if (order.getState().equals(OrderStateEum.DRIVER_CONFIRM.getKey())) {
            //待司机确认 可以更换司机 和 取消订单
            eumList.add(OrderOperateTypeEum.REPLACE);
            eumList.add(OrderOperateTypeEum.CANCEL);
        } else if (order.getState().equals(OrderStateEum.TRANSPORT_RECEIPT.getKey())) {
            eumList.add(OrderOperateTypeEum.ASSIGN);
        }
        orderInfoDto.setOperate(eumList);

        return orderInfoDto;
    }

    @Override
    public List<TaskOrderListDto> getTaskOrderList(Map params) {

        List<TaskOrderListDto> list = orderCommonMapper.getTaskOrderList(params);

        for (TaskOrderListDto o : list) {
            o.setWasteList(orderCommonMapper.getWasteList(o.getId()));
            o.setLoadingAddress(orderAddressMapper.getAddressByOrderId(o.getParentId(), 0));
            o.setUnLoadingAddress(orderAddressMapper.getAddressByOrderId(o.getId(), 1));

            //设置权限
            List<OrderOperateTypeEum> eumList = new ArrayList<>();
            eumList.add(OrderOperateTypeEum.ASSIGN);
            o.setOperate(eumList);

        }

        return list;
    }

    @Override
    public OrderInfoDto getTaskOrderInfo(Map params) {

        return null;
    }
}
